<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Chapter 3 Transforming Data | Data visualisation using R, for researchers who don’t use R</title>
<meta name="author" content="Emily Nordmann, Phil McAleer, Wilhelmiina Toivo, Helena Paterson, Lisa DeBruine">
<meta name="description" content="3.1 Data formats To visualise the experimental reaction time and accuracy data using ggplot2, we first need to reshape the data from wide format to long format. This step can cause friction with...">
<meta name="generator" content="bookdown 0.25 with bs4_book()">
<meta property="og:title" content="Chapter 3 Transforming Data | Data visualisation using R, for researchers who don’t use R">
<meta property="og:type" content="book">
<meta property="og:url" content="https://psyteachr.github.io/introdataviz/transforming-data.html">
<meta property="og:image" content="https://psyteachr.github.io/introdataviz/images/logos/logo.png">
<meta property="og:description" content="3.1 Data formats To visualise the experimental reaction time and accuracy data using ggplot2, we first need to reshape the data from wide format to long format. This step can cause friction with...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Chapter 3 Transforming Data | Data visualisation using R, for researchers who don’t use R">
<meta name="twitter:description" content="3.1 Data formats To visualise the experimental reaction time and accuracy data using ggplot2, we first need to reshape the data from wide format to long format. This step can cause friction with...">
<meta name="twitter:image" content="https://psyteachr.github.io/introdataviz/images/logos/logo.png">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.3.1/transition.js"></script><script src="libs/bs3compat-0.3.1/tabs.js"></script><script src="libs/bs3compat-0.3.1/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=G-6NP3MF25W3"></script><script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-6NP3MF25W3');
    </script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><style type="text/css">
    
    div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
  </style>
<style type="text/css">
    /* Used with Pandoc 2.11+ new --citeproc when CSL is used */
    div.csl-bib-body { }
    div.csl-entry {
      clear: both;
        }
    .hanging div.csl-entry {
      margin-left:2em;
      text-indent:-2em;
    }
    div.csl-left-margin {
      min-width:2em;
      float:left;
    }
    div.csl-right-inline {
      margin-left:2em;
      padding-left:1em;
    }
    div.csl-indent {
      margin-left: 2em;
    }
  </style>
<link rel="stylesheet" href="include/psyteachr.css">
<link rel="stylesheet" href="include/webex.css">
<link rel="stylesheet" href="include/style.css">
</head>
<body data-spy="scroll" data-target="#toc">

<div class="container-fluid">
<div class="row">
  <header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>

    <div class="d-flex align-items-start justify-content-between">
      <h1>
        <a href="index.html" title="">Data visualisation using R, for researchers who don’t use R</a>
      </h1>
      <button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
    </div>

    <div id="main-nav" class="collapse-lg">
      <form role="search">
        <input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>

      <nav aria-label="Table of contents"><h2>Table of contents</h2>
        <ul class="book-toc list-unstyled">
<li><a class="" href="index.html">Overview</a></li>
<li><a class="" href="introduction.html"><span class="header-section-number">1</span> Introduction</a></li>
<li><a class="" href="getting-started.html"><span class="header-section-number">2</span> Getting Started</a></li>
<li><a class="active" href="transforming-data.html"><span class="header-section-number">3</span> Transforming Data</a></li>
<li><a class="" href="representing-summary-statistics.html"><span class="header-section-number">4</span> Representing Summary Statistics</a></li>
<li><a class="" href="multi-part-plots.html"><span class="header-section-number">5</span> Multi-part Plots</a></li>
<li><a class="" href="advanced-plots.html"><span class="header-section-number">6</span> Advanced Plots</a></li>
<li><a class="" href="conclusion.html"><span class="header-section-number">7</span> Conclusion</a></li>
<li class="book-part">Appendices</li>
<li><a class="" href="additional-resources.html"><span class="header-section-number">A</span> Additional resources</a></li>
<li><a class="" href="additional-customisation-options.html"><span class="header-section-number">B</span> Additional customisation options</a></li>
<li><a class="" href="plotstyle.html"><span class="header-section-number">C</span> Styling Plots</a></li>
<li><a class="" href="advanced-plots-1.html"><span class="header-section-number">D</span> Advanced Plots</a></li>
<li><a class="" href="license.html">License</a></li>
<li><a class="" href="references.html">References</a></li>
</ul>

        <div class="book-extra">
          <p><a id="book-repo" href="https://github.com/psyteachr/introdataviz">View book source <i class="fab fa-github"></i></a></p>
        </div>
      </nav>
</div>
  </header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="transforming-data" class="section level1" number="3">
<h1>
<span class="header-section-number">3</span> Transforming Data<a class="anchor" aria-label="anchor" href="#transforming-data"><i class="fas fa-link"></i></a>
</h1>
<div id="data-formats" class="section level2" number="3.1">
<h2>
<span class="header-section-number">3.1</span> Data formats<a class="anchor" aria-label="anchor" href="#data-formats"><i class="fas fa-link"></i></a>
</h2>
<p>To visualise the experimental reaction time and accuracy data using <code>ggplot2</code>, we first need to reshape the data from wide format to long format. This step can cause friction with novice users of R. Traditionally, psychologists have been taught data skills using wide-format data. Wide-format data typically has one row of data for each participant, with separate columns for each score or variable. For repeated-measures variables, the dependent variable is split across different columns. For between-groups variables, a separate column is added to encode the group to which a participant or observation belongs.</p>
<p>The simulated lexical decision data is currently in wide format (see Table <a href="transforming-data.html#tab:wide-data">3.1</a>), where each participant's aggregated <a class="footnote-ref" tabindex="0" data-toggle="popover" data-content='&lt;p&gt;In this tutorial we have chosen to gloss over the data processing steps that must occur to get from the raw data to aggregated values. This type of processing requires a more extensive tutorial than we can provide in the current paper. More importantly, it is still possible to use R for data visualisation having done the preparatory steps using existing workflows in Excel and SPSS. We bypass these initial steps and focus on tangible outputs that may then encourage further mastery of reproducible methods. Collectively we tend to call the steps for reshaping data and for processing raw data or for getting data ready to use statistical functions "wrangling".&lt;/p&gt;'><sup>4</sup></a> reaction time and accuracy for each level of the within-subject variable is split across multiple columns for the repeated factor of conditon (words versus non-words).</p>
<div class="inline-table"><table class="table table-sm">
<caption>
<span id="tab:wide-data">Table 3.1: </span>Data in wide format.
</caption>
<thead><tr>
<th style="text-align:left;">
id
</th>
<th style="text-align:right;">
age
</th>
<th style="text-align:left;">
language
</th>
<th style="text-align:right;">
rt_word
</th>
<th style="text-align:right;">
rt_nonword
</th>
<th style="text-align:right;">
acc_word
</th>
<th style="text-align:right;">
acc_nonword
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:right;">
379.46
</td>
<td style="text-align:right;">
516.82
</td>
<td style="text-align:right;">
99
</td>
<td style="text-align:right;">
90
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:right;">
312.45
</td>
<td style="text-align:right;">
435.04
</td>
<td style="text-align:right;">
94
</td>
<td style="text-align:right;">
82
</td>
</tr>
<tr>
<td style="text-align:left;">
S003
</td>
<td style="text-align:right;">
23
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:right;">
404.94
</td>
<td style="text-align:right;">
458.50
</td>
<td style="text-align:right;">
96
</td>
<td style="text-align:right;">
87
</td>
</tr>
<tr>
<td style="text-align:left;">
S004
</td>
<td style="text-align:right;">
28
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:right;">
298.37
</td>
<td style="text-align:right;">
335.89
</td>
<td style="text-align:right;">
92
</td>
<td style="text-align:right;">
76
</td>
</tr>
<tr>
<td style="text-align:left;">
S005
</td>
<td style="text-align:right;">
26
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:right;">
316.42
</td>
<td style="text-align:right;">
401.32
</td>
<td style="text-align:right;">
91
</td>
<td style="text-align:right;">
83
</td>
</tr>
<tr>
<td style="text-align:left;">
S006
</td>
<td style="text-align:right;">
29
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:right;">
357.17
</td>
<td style="text-align:right;">
367.34
</td>
<td style="text-align:right;">
96
</td>
<td style="text-align:right;">
78
</td>
</tr>
</tbody>
</table></div>
<p>Wide format is popular because it is intuitive to read and easy to enter data into as all the data for one participant is contained within a single row. However, for the purposes of analysis, and particularly for analysis using R, this format is unsuitable. Whilst it is intuitive to read by a human, the same is not true for a computer. Wide-format data concatenates multiple pieces of information in a single column, for example in Table <a href="transforming-data.html#tab:wide-data">3.1</a>, <code>rt_word</code> contains information related to both a DV and one level of an IV. In comparison, long-format data separates the DV from the IVs so that each column represents only one variable. The less intuitive part is that long-format data has multiple rows for each participant (one row for each observation) and a column that encodes the level of the IV (<code>word</code> or <code>nonword</code>). Wickham <span class="citation">(<a href="references.html#ref-wickham2014tidy" role="doc-biblioref">2014</a>)</span> provides a comprehensive overview of the benefits of a similar format known as tidy data, which is a standard way of mapping a dataset to its structure. For the purposes of this tutorial there are two important rules: each column should be a <em>variable</em> and each row should be an <em>observation</em>.</p>
<p>Moving from using wide-format to long-format datasets can require a conceptual shift on the part of the researcher and one that usually only comes with practice and repeated exposure<a class="footnote-ref" tabindex="0" data-toggle="popover" data-content="&lt;p&gt;That is to say, if you are new to R, know that many before you have struggled with this conceptual shift - it does get better, it just takes time and your preferred choice of cursing.&lt;/p&gt;"><sup>5</sup></a>. It may be helpful to make a note that “row = participant” (wide format) and “row = observation” (long format) until you get used to moving between the formats. For our example dataset, adhering to these rules for reshaping the data would produce Table <a href="transforming-data.html#tab:long">3.2</a>. Rather than different observations of the same dependent variable being split across columns, there is now a single column for the DV reaction time, and a single column for the DV accuracy. Each participant now has multiple rows of data, one for each observation (i.e., for each participant there will be as many rows as there are levels of the within-subject IV). Although there is some repetition of age and language group, each row is unique when looking at the combination of measures.</p>
<div class="inline-table"><table class="table table-sm">
<caption>
<span id="tab:long">Table 3.2: </span>Data in the correct format for visualization.
</caption>
<thead><tr>
<th style="text-align:left;">
id
</th>
<th style="text-align:right;">
age
</th>
<th style="text-align:left;">
language
</th>
<th style="text-align:left;">
condition
</th>
<th style="text-align:right;">
rt
</th>
<th style="text-align:right;">
acc
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
word
</td>
<td style="text-align:right;">
379.46
</td>
<td style="text-align:right;">
99
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
nonword
</td>
<td style="text-align:right;">
516.82
</td>
<td style="text-align:right;">
90
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
word
</td>
<td style="text-align:right;">
312.45
</td>
<td style="text-align:right;">
94
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
nonword
</td>
<td style="text-align:right;">
435.04
</td>
<td style="text-align:right;">
82
</td>
</tr>
<tr>
<td style="text-align:left;">
S003
</td>
<td style="text-align:right;">
23
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
word
</td>
<td style="text-align:right;">
404.94
</td>
<td style="text-align:right;">
96
</td>
</tr>
<tr>
<td style="text-align:left;">
S003
</td>
<td style="text-align:right;">
23
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
nonword
</td>
<td style="text-align:right;">
458.50
</td>
<td style="text-align:right;">
87
</td>
</tr>
</tbody>
</table></div>
<p>The benefits and flexibility of this format will hopefully become apparent as we progress through the tutorial, however, a useful rule of thumb when working with data in R for visualisation is that <em>anything that shares an axis should probably be in the same column</em>. For example, a simple boxplot showing reaction time by condition would display the variable <code>condition</code> on the x-axis with bars representing both the <code>word</code> and <code>nonword</code> data, and <code>rt</code> on the y-axis. Therefore, all the data relating to <code>condition</code> should be in one column, and all the data relating to <code>rt</code> should be in a separate single column, rather than being split like in wide-format data.</p>
</div>
<div id="wide-to-long-format" class="section level2" number="3.2">
<h2>
<span class="header-section-number">3.2</span> Wide to long format<a class="anchor" aria-label="anchor" href="#wide-to-long-format"><i class="fas fa-link"></i></a>
</h2>
<p>We have chosen a 2 x 2 design with two DVs, as we anticipate that this is a design many researchers will be familiar with and may also have existing datasets with a similar structure. However, it is worth normalising that trial-and-error is part of the process of learning how to apply these functions to new datasets and structures. Data visualisation can be a useful way to scaffold learning these data transformations because they can provide a concrete visual check as to whether you have done what you intended to do with your data.</p>
<div id="step-1-pivot_longer" class="section level3" number="3.2.1">
<h3>
<span class="header-section-number">3.2.1</span> Step 1: <code>pivot_longer()</code><a class="anchor" aria-label="anchor" href="#step-1-pivot_longer"><i class="fas fa-link"></i></a>
</h3>
<p>The first step is to use the function <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code> to transform the data to long-form. We have purposefully used a more complex dataset with two DVs for this tutorial to aid researchers applying our code to their own datasets. Because of this, we will break down the steps involved to help show how the code works.</p>
<p>This first code ignores that the dataset has two DVs, a problem we will fix in step 2. The pivot functions can be easier to show than tell - you may find it a useful exercise to run the below code and compare the newly created object <code>long</code> (Table <a href="transforming-data.html#tab:long1-example">3.3</a>) with the original <code>dat</code> Table <a href="transforming-data.html#tab:wide-data">3.1</a> before reading on.</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">long</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">dat</span>, 
                     cols <span class="op">=</span> <span class="va">rt_word</span><span class="op">:</span><span class="va">acc_nonword</span>, 
                     names_to <span class="op">=</span> <span class="st">"dv_condition"</span>,
                     values_to <span class="op">=</span> <span class="st">"dv"</span><span class="op">)</span></code></pre></div>
<ul>
<li><p>As with the other tidyverse functions, the first argument specifies the dataset to use as the base, in this case <code>dat</code>. This argument name is often dropped in examples.</p></li>
<li><p><code>cols</code> specifies all the columns you want to transform. The easiest way to visualise this is to think about which columns would be the same in the new long-form dataset and which will change. If you refer back to Table <a href="transforming-data.html#tab:wide-data">3.1</a>, you can see that <code>id</code>, <code>age</code>, and <code>language</code> all remain, while the columns that contain the measurements of the DVs change. The colon notation <code>first_column:last_column</code> is used to select all variables from the first column specified to the last In our code, <code>cols</code> specifies that the columns we want to transform are <code>rt_word</code> to <code>acc_nonword</code>.</p></li>
<li><p><code>names_to</code> specifies the name of the new column that will be created. This column will contain the names of the selected existing columns.</p></li>
<li><p>Finally, <code>values_to</code> names the new column that will contain the values in the selected columns. In this case we'll call it <code>dv</code>.</p></li>
</ul>
<p>At this point you may find it helpful to go back and compare <code>dat</code> and <code>long</code> again to see how each argument matches up with the output of the table.</p>
<div class="inline-table"><table class="table table-sm">
<caption>
<span id="tab:long1-example">Table 3.3: </span>Data in long format with mixed DVs.
</caption>
<thead><tr>
<th style="text-align:left;">
id
</th>
<th style="text-align:right;">
age
</th>
<th style="text-align:left;">
language
</th>
<th style="text-align:left;">
dv_condition
</th>
<th style="text-align:right;">
dv
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt_word
</td>
<td style="text-align:right;">
379.46
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt_nonword
</td>
<td style="text-align:right;">
516.82
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
acc_word
</td>
<td style="text-align:right;">
99.00
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
acc_nonword
</td>
<td style="text-align:right;">
90.00
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt_word
</td>
<td style="text-align:right;">
312.45
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt_nonword
</td>
<td style="text-align:right;">
435.04
</td>
</tr>
</tbody>
</table></div>
</div>
<div id="step-2-pivot_longer-adjusted" class="section level3" number="3.2.2">
<h3>
<span class="header-section-number">3.2.2</span> Step 2: <code>pivot_longer()</code> adjusted<a class="anchor" aria-label="anchor" href="#step-2-pivot_longer-adjusted"><i class="fas fa-link"></i></a>
</h3>
<p>The problem with the above long-format data-set is that <code>dv_condition</code> combines two variables - it has information about the type of DV and the condition of the IV. To account for this, we include a new argument <code>names_sep</code> and adjust <code>name_to</code> to specify the creation of two new columns. Note that we are pivoting the same wide-format dataset <code>dat</code> as we did in step 1.</p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">long2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">dat</span>, 
                     cols <span class="op">=</span> <span class="va">rt_word</span><span class="op">:</span><span class="va">acc_nonword</span>, 
                     names_sep <span class="op">=</span> <span class="st">"_"</span>, 
                     names_to <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"dv_type"</span>, <span class="st">"condition"</span><span class="op">)</span>,
                     values_to <span class="op">=</span> <span class="st">"dv"</span><span class="op">)</span></code></pre></div>
<ul>
<li><p><code>names_sep</code> specifies how to split up the variable name in cases where it has multiple components. This is when taking care to name your variables consistently and meaningfully pays off. Because the word to the left of the separator (<code>_</code>) is always the DV type and the word to the right is always the condition of the within-subject IV, it is easy to automatically split the columns.</p></li>
<li><p>Note that when specifying more than one column name, they must be combined using <code><a href="https://rdrr.io/r/base/c.html">c()</a></code> and be enclosed in their own quotation marks.</p></li>
</ul>
<div class="inline-table"><table class="table table-sm">
<caption>
<span id="tab:long-example">Table 3.4: </span>Data in long format with dv type and condition in separate columns.
</caption>
<thead><tr>
<th style="text-align:left;">
id
</th>
<th style="text-align:right;">
age
</th>
<th style="text-align:left;">
language
</th>
<th style="text-align:left;">
dv_type
</th>
<th style="text-align:left;">
condition
</th>
<th style="text-align:right;">
dv
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt
</td>
<td style="text-align:left;">
word
</td>
<td style="text-align:right;">
379.46
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt
</td>
<td style="text-align:left;">
nonword
</td>
<td style="text-align:right;">
516.82
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
acc
</td>
<td style="text-align:left;">
word
</td>
<td style="text-align:right;">
99.00
</td>
</tr>
<tr>
<td style="text-align:left;">
S001
</td>
<td style="text-align:right;">
22
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
acc
</td>
<td style="text-align:left;">
nonword
</td>
<td style="text-align:right;">
90.00
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt
</td>
<td style="text-align:left;">
word
</td>
<td style="text-align:right;">
312.45
</td>
</tr>
<tr>
<td style="text-align:left;">
S002
</td>
<td style="text-align:right;">
33
</td>
<td style="text-align:left;">
monolingual
</td>
<td style="text-align:left;">
rt
</td>
<td style="text-align:left;">
nonword
</td>
<td style="text-align:right;">
435.04
</td>
</tr>
</tbody>
</table></div>
</div>
<div id="step-3-pivot_wider" class="section level3" number="3.2.3">
<h3>
<span class="header-section-number">3.2.3</span> Step 3: <code>pivot_wider()</code><a class="anchor" aria-label="anchor" href="#step-3-pivot_wider"><i class="fas fa-link"></i></a>
</h3>
<p>Although we have now split the columns so that there are separate variables for the DV type and level of condition, because the two DVs are different types of data, there is an additional bit of wrangling required to get the data in the right format for plotting.</p>
<p>In the current long-format dataset, the column <code>dv</code> contains both reaction time and accuracy measures. Keeping in mind the rule of thumb that <em>anything that shares an axis should probably be in the same column,</em> this creates a problem because we cannot plot two different units of measurement on the same axis. To fix this we need to use the function <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code>. Again, we would encourage you at this point to compare <code>long2</code> and <code>dat_long</code> with the below code to try and map the connections before reading on.</p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">dat_long</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider</a></span><span class="op">(</span><span class="va">long2</span>, 
                        names_from <span class="op">=</span> <span class="st">"dv_type"</span>, 
                        values_from <span class="op">=</span> <span class="st">"dv"</span><span class="op">)</span></code></pre></div>
<ul>
<li><p>The first argument is again the dataset you wish to work from, in this case <code>long2</code>. We have removed the argument name <code>data</code> in this example.</p></li>
<li><p><code>names_from</code> is the reverse of <code>names_to</code> from <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code>. It will take the values from the variable specified and use these as the new column names. In this case, the values of <code>rt</code> and <code>acc</code> that are currently in the <code>dv_type</code> column will become the new column names.</p></li>
<li><p><code>values_from</code> is the reverse of <code>values_to</code> from <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code>. It specifies the column that contains the values to fill the new columns with. In this case, the new columns <code>rt</code> and <code>acc</code> will be filled with the values that were in <code>dv</code>.</p></li>
</ul>
<p>Again, it can be helpful to compare each dataset with the code to see how it aligns. This final long-form data should look like Table <a href="transforming-data.html#tab:long">3.2</a>.</p>
<p>If you are working with a dataset with only one DV, note that only step 1 of this process would be necessary. Also, be careful not to calculate demographic descriptive statistics from this long-form dataset. Because the process of transformation has introduced some repetition for these variables, the wide-format dataset where one row equals one participant should be used for demographic information. Finally, the three step process noted above is broken down for teaching purposes, in reality, one would likely do this in a single pipeline of code, for example:</p>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">dat_long</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">dat</span>, 
                         cols <span class="op">=</span> <span class="va">rt_word</span><span class="op">:</span><span class="va">acc_nonword</span>, 
                         names_sep <span class="op">=</span> <span class="st">"_"</span>, 
                         names_to <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"dv_type"</span>, <span class="st">"condition"</span><span class="op">)</span>,
                         values_to <span class="op">=</span> <span class="st">"dv"</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span>
  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider</a></span><span class="op">(</span>names_from <span class="op">=</span> <span class="st">"dv_type"</span>, 
              values_from <span class="op">=</span> <span class="st">"dv"</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="histogram-2" class="section level2" number="3.3">
<h2>
<span class="header-section-number">3.3</span> Histogram 2<a class="anchor" aria-label="anchor" href="#histogram-2"><i class="fas fa-link"></i></a>
</h2>
<p>Now that we have the experimental data in the right form, we can begin to create some useful visualizations. First, to demonstrate how code recipes can be reused and adapted, we will create histograms of reaction time and accuracy. The below code uses the same template as before but changes the dataset (<code>dat_long</code>), the bin-widths of the histograms, the <code>x</code> variable to display (<code>rt</code>/<code>acc</code>), and the name of the x-axis.</p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram</a></span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">10</span>, fill <span class="op">=</span> <span class="st">"white"</span>, colour <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span>

<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">acc</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram</a></span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">1</span>, fill <span class="op">=</span> <span class="st">"white"</span>, colour <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Accuracy (0-100)"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:histograms"></span>
<img src="03-ch3_files/figure-html/histograms-1.png" alt="Histograms showing the distribution of reaction time (top) and accuracy (bottom)" width="80%"><p class="caption">
Figure 3.1: Histograms showing the distribution of reaction time (top) and accuracy (bottom)
</p>
</div>
</div>
<div id="density-plots" class="section level2" number="3.4">
<h2>
<span class="header-section-number">3.4</span> Density plots<a class="anchor" aria-label="anchor" href="#density-plots"><i class="fas fa-link"></i></a>
</h2>
<p>The layer system makes it easy to create new types of plots by adapting existing recipes. For example, rather than creating a histogram, we can create a smoothed density plot by calling <code><a href="https://ggplot2.tidyverse.org/reference/geom_density.html">geom_density()</a></code> rather than <code><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram()</a></code>. The rest of the code remains identical.</p>
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_density.html">geom_density</a></span><span class="op">(</span><span class="op">)</span><span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:density-rt"></span>
<img src="03-ch3_files/figure-html/density-rt-1.png" alt="Density plot of reaction time." width="80%"><p class="caption">
Figure 3.2: Density plot of reaction time.
</p>
</div>
<div id="grouped-density-plots" class="section level3" number="3.4.1">
<h3>
<span class="header-section-number">3.4.1</span> Grouped density plots<a class="anchor" aria-label="anchor" href="#grouped-density-plots"><i class="fas fa-link"></i></a>
</h3>
<p>Density plots are most useful for comparing the distributions of different groups of data. Because the dataset is now in long format, with each variable contained within a single column, we can map <code>condition</code> to the plot.</p>
<ul>
<li>In addition to mapping <code>rt</code> to the x-axis, we specify the <code>fill</code> aesthetic to fill the visualisation so that each level of the <code>condition</code> variable is represented by a different colour.</li>
<li>Because the density plots are overlapping, we set <code>alpha = 0.75</code> to make the geoms 75% transparent.</li>
<li>As with the x and y-axis scale functions, we can edit the names and labels of our fill aesthetic by adding on another <code>scale_*</code> layer (<code><a href="https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html">scale_fill_discrete()</a></code>).</li>
<li>Note that the <code>fill</code> here is set inside the <code><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes()</a></code> function, which tells ggplot to set the fill differently for each value in the <code>condition</code> column. You cannot specify which colour here (e.g., <code>fill="red"</code>), like you could when you set <code>fill</code> inside the <code>geom_*()</code> function before.</li>
</ul>
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_density.html">geom_density</a></span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">0.75</span><span class="op">)</span><span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html">scale_fill_discrete</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Condition"</span>,
                      labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Non-Word"</span>, <span class="st">"Word"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:density-grouped"></span>
<img src="03-ch3_files/figure-html/density-grouped-1.png" alt="Density plot of reaction times grouped by condition." width="80%"><p class="caption">
Figure 3.3: Density plot of reaction times grouped by condition.
</p>
</div>
<div class="webex-solution">
<button>
Correction to paper
</button>
<p>Please note that the code and figure for this plot has been corrected from the published paper due to the labels "Word" and "Non-word" being incorrectly reversed. This is of course mortifying as authors, although it does provide a useful teachable moment that R will do what you tell it to do, no more, no less, regardless of whether what you tell it to do is wrong.</p>
</div>
</div>
</div>
<div id="scatterplots" class="section level2" number="3.5">
<h2>
<span class="header-section-number">3.5</span> Scatterplots<a class="anchor" aria-label="anchor" href="#scatterplots"><i class="fas fa-link"></i></a>
</h2>
<p>Scatterplots are created by calling <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code> and require both an <code>x</code> and <code>y</code> variable to be specified in the mapping.</p>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:point-plot"></span>
<img src="03-ch3_files/figure-html/point-plot-1.png" alt="Scatterplot of reaction time versus age." width="80%"><p class="caption">
Figure 3.4: Scatterplot of reaction time versus age.
</p>
</div>
<p>A line of best fit can be added with an additional layer that calls the function <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code>. The default is to draw a LOESS or curved regression line. However, a linear line of best fit can be specified using <code>method = "lm"</code>. By default, <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code> will also draw a confidence envelope around the regression line; this can be removed by adding <code>se = FALSE</code> to <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code>. A common error is to try and use <code><a href="https://ggplot2.tidyverse.org/reference/geom_path.html">geom_line()</a></code> to draw the line of best fit, which whilst a sensible guess, will not work (try it).</p>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:smooth-plot"></span>
<img src="03-ch3_files/figure-html/smooth-plot-1.png" alt="Line of best fit for reaction time versus age." width="80%"><p class="caption">
Figure 3.5: Line of best fit for reaction time versus age.
</p>
</div>
<div id="grouped-scatterplots" class="section level3" number="3.5.1">
<h3>
<span class="header-section-number">3.5.1</span> Grouped scatterplots<a class="anchor" aria-label="anchor" href="#grouped-scatterplots"><i class="fas fa-link"></i></a>
</h3>
<p>Similar to the density plot, the scatterplot can also be easily adjusted to display grouped data. For <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code>, the grouping variable is mapped to <code>colour</code> rather than <code>fill</code> and the relevant <code>scale_*</code> function is added.</p>
<div class="sourceCode" id="cb35"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span>, colour <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html">scale_colour_discrete</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Condition"</span>,
                      labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Non-Word"</span>, <span class="st">"Word"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:scatter-grouped"></span>
<img src="03-ch3_files/figure-html/scatter-grouped-1.png" alt="Grouped scatterplot of reaction time versus age by condition." width="80%"><p class="caption">
Figure 3.6: Grouped scatterplot of reaction time versus age by condition.
</p>
</div>
<div class="webex-solution">
<button>
Correction to paper
</button>
<p>Please note that the code and figure for this plot has been corrected from the published paper due to the labels "Word" and "Non-word" being incorrectly reversed. This is of course mortifying as authors, although it does provide a useful teachable moment that R will do what you tell it to do, no more, no less, regardless of whether what you tell it to do is wrong.</p>
</div>
</div>
</div>
<div id="long-to-wide-format" class="section level2" number="3.6">
<h2>
<span class="header-section-number">3.6</span> Long to wide format<a class="anchor" aria-label="anchor" href="#long-to-wide-format"><i class="fas fa-link"></i></a>
</h2>
<p>Following the rule that <em>anything that shares an axis should probably be in the same column</em> means that we will frequently need our data in long-form when using <code>ggplot2</code>, However, there are some cases when wide format is necessary. For example, we may wish to visualise the relationship between reaction time in the word and non-word conditions. This requires that the corresponding word and non-word values for each participant be in the same row. The easiest way to achieve this in our case would simply be to use the original wide-format data as the input:</p>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt_word</span>, y <span class="op">=</span> <span class="va">rt_nonword</span>, colour <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:unnamed-chunk-5"></span>
<img src="03-ch3_files/figure-html/unnamed-chunk-5-1.png" alt="Scatterplot with data grouped by language group" width="80%"><p class="caption">
Figure 3.7: Scatterplot with data grouped by language group
</p>
</div>
<p>However, there may also be cases when you do not have an original wide-format version and you can use the <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> function to transform from long to wide.</p>
<div class="sourceCode" id="cb37"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">dat_wide</span> <span class="op">&lt;-</span> <span class="va">dat_long</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span>
  <span class="fu"><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider</a></span><span class="op">(</span>id_cols <span class="op">=</span> <span class="st">"id"</span>,
              names_from <span class="op">=</span> <span class="st">"condition"</span>, 
              values_from <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="va">rt</span>,<span class="va">acc</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="inline-table"><table class="table table-sm">
<thead><tr>
<th style="text-align:center;">
id
</th>
<th style="text-align:center;">
rt_word
</th>
<th style="text-align:center;">
rt_nonword
</th>
<th style="text-align:center;">
acc_word
</th>
<th style="text-align:center;">
acc_nonword
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center;">
S001
</td>
<td style="text-align:center;">
379.4585
</td>
<td style="text-align:center;">
516.8176
</td>
<td style="text-align:center;">
99
</td>
<td style="text-align:center;">
90
</td>
</tr>
<tr>
<td style="text-align:center;">
S002
</td>
<td style="text-align:center;">
312.4513
</td>
<td style="text-align:center;">
435.0404
</td>
<td style="text-align:center;">
94
</td>
<td style="text-align:center;">
82
</td>
</tr>
<tr>
<td style="text-align:center;">
S003
</td>
<td style="text-align:center;">
404.9407
</td>
<td style="text-align:center;">
458.5022
</td>
<td style="text-align:center;">
96
</td>
<td style="text-align:center;">
87
</td>
</tr>
<tr>
<td style="text-align:center;">
S004
</td>
<td style="text-align:center;">
298.3734
</td>
<td style="text-align:center;">
335.8933
</td>
<td style="text-align:center;">
92
</td>
<td style="text-align:center;">
76
</td>
</tr>
<tr>
<td style="text-align:center;">
S005
</td>
<td style="text-align:center;">
316.4250
</td>
<td style="text-align:center;">
401.3214
</td>
<td style="text-align:center;">
91
</td>
<td style="text-align:center;">
83
</td>
</tr>
<tr>
<td style="text-align:center;">
S006
</td>
<td style="text-align:center;">
357.1710
</td>
<td style="text-align:center;">
367.3355
</td>
<td style="text-align:center;">
96
</td>
<td style="text-align:center;">
78
</td>
</tr>
</tbody>
</table></div>
</div>
<div id="customisation-2" class="section level2" number="3.7">
<h2>
<span class="header-section-number">3.7</span> Customisation 2<a class="anchor" aria-label="anchor" href="#customisation-2"><i class="fas fa-link"></i></a>
</h2>
<div id="accessible-colour-schemes" class="section level3" number="3.7.1">
<h3>
<span class="header-section-number">3.7.1</span> Accessible colour schemes<a class="anchor" aria-label="anchor" href="#accessible-colour-schemes"><i class="fas fa-link"></i></a>
</h3>
<p>One of the drawbacks of using <code>ggplot2</code> for visualisation is that the default colour scheme is not accessible (or visually appealing). The red and green default palette is difficult for colour-blind people to differentiate, and also does not display well in greyscale. You can specify exact custom colours for your plots, but one easy option is to use a custom colour palette. These take the same arguments as their default <code>scale</code> sister functions for updating axis names and labels, but display plots in contrasting colours that can be read by colour-blind people and that also print well in grey scale. For categorical colours, the "Set2", "Dark2" and "Paired" palettes from the <code>brewer</code> scale functions are colourblind-safe (but are hard to distinhuish in greyscale). For continuous colours, such as when colour is representing the magnitude of a correlation in a tile plot, the <code>viridis</code> scale functions provide a number of different colourblind and greyscale-safe options.</p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span>, colour <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html">scale_color_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Dark2"</span>,
                     name <span class="op">=</span> <span class="st">"Condition"</span>,
                     labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Non-word"</span>, <span class="st">"Word"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:brewer"></span>
<img src="03-ch3_files/figure-html/brewer-1.png" alt="Use the Dark2 brewer colour scheme for accessibility." width="80%"><p class="caption">
Figure 3.8: Use the Dark2 brewer colour scheme for accessibility.
</p>
</div>
<div class="webex-solution">
<button>
Correction to paper
</button>
<p>Please note that the code and figure for this plot has been corrected from the published paper due to the labels "Word" and "Non-word" being incorrectly reversed. This is of course mortifying as authors, although it does provide a useful teachable moment that R will do what you tell it to do, no more, no less, regardless of whether what you tell it to do is wrong.</p>
</div>
</div>
<div id="specifying-axis-breaks-with-seq" class="section level3" number="3.7.2">
<h3>
<span class="header-section-number">3.7.2</span> Specifying axis <code>breaks</code> with <code>seq()</code><a class="anchor" aria-label="anchor" href="#specifying-axis-breaks-with-seq"><i class="fas fa-link"></i></a>
</h3>
<p>Previously, when we have edited the <code>breaks</code> on the axis labels, we have done so manually, typing out all the values we want to display on the axis. For example, the below code edits the y-axis so that <code>age</code> is displayed in increments of 5.</p>
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">20</span>,<span class="fl">25</span>,<span class="fl">30</span>,<span class="fl">35</span>,<span class="fl">40</span>,<span class="fl">45</span>,<span class="fl">50</span>,<span class="fl">55</span>,<span class="fl">60</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<p>However, this is somewhat inefficient. Instead, we can use the function <code><a href="https://rdrr.io/r/base/seq.html">seq()</a></code> (short for sequence) to specify the first and last value and the increments <code>by</code> which the breaks should display between these two values.</p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>breaks <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op">(</span><span class="fl">20</span>,<span class="fl">60</span>, by <span class="op">=</span> <span class="fl">5</span><span class="op">)</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="activities-2" class="section level2" number="3.8">
<h2>
<span class="header-section-number">3.8</span> Activities 2<a class="anchor" aria-label="anchor" href="#activities-2"><i class="fas fa-link"></i></a>
</h2>
<p>Before you move on try the following:</p>
<ol style="list-style-type: decimal">
<li>Use <code>fill</code> to created grouped histograms that display the distributions for <code>rt</code> for each <code>language</code> group separately and also edit the fill axis labels. Try adding <code>position = "dodge"</code> to <code><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram()</a></code> to see what happens.</li>
</ol>
<div class="webex-solution">
<button>
Solution 1
</button>
<div class="sourceCode" id="cb41"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># fill and axis changes</span>
<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram</a></span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">10</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html">scale_fill_discrete</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Group"</span>,
                      labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Monolingual"</span>, <span class="st">"Bilingual"</span><span class="op">)</span><span class="op">)</span>
<span class="co"># add in dodge</span>
<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">language</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html">geom_histogram</a></span><span class="op">(</span>binwidth <span class="op">=</span> <span class="fl">10</span>, position <span class="op">=</span> <span class="st">"dodge"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html">scale_fill_discrete</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Group"</span>,
                      labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Monolingual"</span>, <span class="st">"Bilingual"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
</div>
<ol start="2" style="list-style-type: decimal">
<li>Use <code>scale_*</code> functions to edit the name of the x and y-axis on the scatterplot</li>
</ol>
<div class="webex-solution">
<button>
Solution 2
</button>
<div class="sourceCode" id="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Age"</span><span class="op">)</span></code></pre></div>
</div>
<ol start="3" style="list-style-type: decimal">
<li>Use <code>se = FALSE</code> to remove the confidence envelope from the scatterplots</li>
</ol>
<div class="webex-solution">
<button>
Solution 3
</button>
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span>method <span class="op">=</span> <span class="st">"lm"</span>, se <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Age"</span><span class="op">)</span></code></pre></div>
</div>
<ol start="4" style="list-style-type: decimal">
<li>Remove <code>method = "lm"</code> from <code><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth()</a></code> to produce a curved fit line.</li>
</ol>
<div class="webex-solution">
<button>
Solution 4
</button>
<div class="sourceCode" id="cb44"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, y <span class="op">=</span> <span class="va">age</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_y_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Age"</span><span class="op">)</span></code></pre></div>
</div>
<ol start="5" style="list-style-type: decimal">
<li>Replace the default fill on the grouped density plot with a colour-blind friendly version.</li>
</ol>
<div class="webex-solution">
<button>
Solution
</button>
<div class="sourceCode" id="cb45"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dat_long</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">rt</span>, fill <span class="op">=</span> <span class="va">condition</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_density.html">geom_density</a></span><span class="op">(</span>alpha <span class="op">=</span> <span class="fl">0.75</span><span class="op">)</span><span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_x_continuous</a></span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Reaction time (ms)"</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_brewer.html">scale_fill_brewer</a></span><span class="op">(</span>palette <span class="op">=</span> <span class="st">"Set2"</span>, <span class="co"># or "Dark2" or "Paired"</span>
                    name <span class="op">=</span> <span class="st">"Condition"</span>,
                    labels <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Non-word"</span>, <span class="st">"Word"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
</div>

</div>
</div>

<script>

/* update total correct if #webex-total_correct exists */
update_total_correct = function() {
  console.log("webex: update total_correct");

  if (t = document.getElementById("webex-total_correct")) {
    var correct = document.getElementsByClassName("webex-correct").length;
    var solvemes = document.getElementsByClassName("webex-solveme").length;
    var radiogroups = document.getElementsByClassName("webex-radiogroup").length;
    var selects = document.getElementsByClassName("webex-select").length;
    
    t.innerHTML = correct + " of " + (solvemes + radiogroups + selects) + " correct";
  }
}

/* webex-solution button toggling function */
b_func = function() {
  console.log("webex: toggle hide");
  
  var cl = this.parentElement.classList;
  if (cl.contains('open')) {
    cl.remove("open");
  } else {
    cl.add("open");
  }
}

/* function for checking solveme answers */
solveme_func = function(e) {
  console.log("webex: check solveme");

  var real_answers = JSON.parse(this.dataset.answer);
  var my_answer = this.value;
  var cl = this.classList;
  if (cl.contains("ignorecase")) {
    my_answer = my_answer.toLowerCase();
  }
  if (cl.contains("nospaces")) {
    my_answer = my_answer.replace(/ /g, "")
  }

  if (my_answer == "") {
    cl.remove("webex-correct");
    cl.remove("webex-incorrect");
  } else if (real_answers.includes(my_answer)) {
    cl.add("webex-correct");
    cl.remove("webex-incorrect");
  } else {
    cl.add("webex-incorrect");
    cl.remove("webex-correct");
  }

  // match numeric answers within a specified tolerance
  if(this.dataset.tol > 0){
    var tol = JSON.parse(this.dataset.tol);
    var matches = real_answers.map(x => Math.abs(x - my_answer) < tol)
    if (matches.reduce((a, b) => a + b, 0) > 0) {
      cl.add("webex-correct");
    } else {
      cl.remove("webex-correct");
    }
  }

  // added regex bit
  if (cl.contains("regex")){
    answer_regex = RegExp(real_answers.join("|"))
    if (answer_regex.test(my_answer)) {
      cl.add("webex-correct");
    }
  }

  update_total_correct();
}

/* function for checking select answers */
select_func = function(e) {
  console.log("webex: check select");
  
  var cl = this.classList
  
  /* add style */
  cl.remove("webex-incorrect");
  cl.remove("webex-correct");
  if (this.value == "answer") {
    cl.add("webex-correct");
  } else if (this.value != "blank") {
    cl.add("webex-incorrect");
  }
  
  update_total_correct();
}

/* function for checking radiogroups answers */
radiogroups_func = function(e) {
  console.log("webex: check radiogroups");

  var checked_button = document.querySelector('input[name=' + this.id + ']:checked');
  var cl = checked_button.parentElement.classList;
  var labels = checked_button.parentElement.parentElement.children;
  
  /* get rid of styles */
  for (i = 0; i < labels.length; i++) {
    labels[i].classList.remove("webex-incorrect");
    labels[i].classList.remove("webex-correct");
  }
  
  /* add style */
  if (checked_button.value == "answer") {
    cl.add("webex-correct");
  } else {
    cl.add("webex-incorrect");
  }
  
  update_total_correct();
}

window.onload = function() {
  console.log("onload");
  /* set up solution buttons */
  var buttons = document.getElementsByTagName("button");

  for (var i = 0; i < buttons.length; i++) {
    if (buttons[i].parentElement.classList.contains('webex-solution')) {
      buttons[i].onclick = b_func;
    }
  }

  /* set up webex-solveme inputs */
  var solveme = document.getElementsByClassName("webex-solveme");

  for (var i = 0; i < solveme.length; i++) {
    /* make sure input boxes don't auto-anything */
    solveme[i].setAttribute("autocomplete","off");
    solveme[i].setAttribute("autocorrect", "off");
    solveme[i].setAttribute("autocapitalize", "off");
    solveme[i].setAttribute("spellcheck", "false");
    solveme[i].value = "";

    /* adjust answer for ignorecase or nospaces */
    var cl = solveme[i].classList;
    var real_answer = solveme[i].dataset.answer;
    if (cl.contains("ignorecase")) {
      real_answer = real_answer.toLowerCase();
    }
    if (cl.contains("nospaces")) {
      real_answer = real_answer.replace(/ /g, "");
    }
    solveme[i].dataset.answer = real_answer;

    /* attach checking function */
    solveme[i].onkeyup = solveme_func;
    solveme[i].onchange = solveme_func;
  }
  
  /* set up radiogroups */
  var radiogroups = document.getElementsByClassName("webex-radiogroup");
  for (var i = 0; i < radiogroups.length; i++) {
    radiogroups[i].onchange = radiogroups_func;
  }
  
  /* set up selects */
  var selects = document.getElementsByClassName("webex-select");
  for (var i = 0; i < selects.length; i++) {
    selects[i].onchange = select_func;
  }

  update_total_correct();
}

</script><script>
$( document ).ready(function() {
  var cite = ' ';
  var psyteachr = ' <a href="https://psyteachr.github.io/"><img src="images/logos/psyteachr_logo.png" style="height: 31px; color: white;" alt="psyTeachR: Reproducible Research" /></a>';
  var license = ' <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/" target="blank"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png"></a>';

  $("footer div.row div:eq(1) p").html(
    psyteachr + license + cite
  );

  function move_sidebar() {
    var w = window.innerWidth;
    if (w < 992) {
      $("#toc").appendTo($("#main-nav"));
    } else {
      $("#toc").appendTo($("div.sidebar-chapter"));
    }
  }
  move_sidebar();
  window.onresize = move_sidebar;
});
</script><div class="chapter-nav">
<div class="prev"><a href="getting-started.html"><span class="header-section-number">2</span> Getting Started</a></div>
<div class="next"><a href="representing-summary-statistics.html"><span class="header-section-number">4</span> Representing Summary Statistics</a></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
    <nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
      <ul class="nav navbar-nav">
<li><a class="nav-link" href="#transforming-data"><span class="header-section-number">3</span> Transforming Data</a></li>
<li><a class="nav-link" href="#data-formats"><span class="header-section-number">3.1</span> Data formats</a></li>
<li>
<a class="nav-link" href="#wide-to-long-format"><span class="header-section-number">3.2</span> Wide to long format</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#step-1-pivot_longer"><span class="header-section-number">3.2.1</span> Step 1: pivot_longer()</a></li>
<li><a class="nav-link" href="#step-2-pivot_longer-adjusted"><span class="header-section-number">3.2.2</span> Step 2: pivot_longer() adjusted</a></li>
<li><a class="nav-link" href="#step-3-pivot_wider"><span class="header-section-number">3.2.3</span> Step 3: pivot_wider()</a></li>
</ul>
</li>
<li><a class="nav-link" href="#histogram-2"><span class="header-section-number">3.3</span> Histogram 2</a></li>
<li>
<a class="nav-link" href="#density-plots"><span class="header-section-number">3.4</span> Density plots</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#grouped-density-plots"><span class="header-section-number">3.4.1</span> Grouped density plots</a></li></ul>
</li>
<li>
<a class="nav-link" href="#scatterplots"><span class="header-section-number">3.5</span> Scatterplots</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#grouped-scatterplots"><span class="header-section-number">3.5.1</span> Grouped scatterplots</a></li></ul>
</li>
<li><a class="nav-link" href="#long-to-wide-format"><span class="header-section-number">3.6</span> Long to wide format</a></li>
<li>
<a class="nav-link" href="#customisation-2"><span class="header-section-number">3.7</span> Customisation 2</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#accessible-colour-schemes"><span class="header-section-number">3.7.1</span> Accessible colour schemes</a></li>
<li><a class="nav-link" href="#specifying-axis-breaks-with-seq"><span class="header-section-number">3.7.2</span> Specifying axis breaks with seq()</a></li>
</ul>
</li>
<li><a class="nav-link" href="#activities-2"><span class="header-section-number">3.8</span> Activities 2</a></li>
</ul>

      <div class="book-extra">
        <ul class="list-unstyled">
<li><a id="book-source" href="https://github.com/psyteachr/introdataviz/blob/master/book/03-ch3.Rmd">View source <i class="fab fa-github"></i></a></li>
          <li><a id="book-edit" href="https://github.com/psyteachr/introdataviz/edit/master/book/03-ch3.Rmd">Edit this page <i class="fab fa-github"></i></a></li>
        </ul>
</div>
    </nav>
</div>

</div>
</div> <!-- .container -->

<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">

  <div class="col-12 col-md-6 mt-3">
    <p>"<strong>Data visualisation using R, for researchers who don’t use R</strong>" was written by Emily Nordmann, Phil McAleer, Wilhelmiina Toivo, Helena Paterson, Lisa DeBruine. It was last built on 2022-05-02.</p>
  </div>

  <div class="col-12 col-md-6 mt-3">
    <p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
  </div>

</div></div>
</footer>
</body>
</html>
